package GUI_3GE;


import exeptions.ControlledUserException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import pkg3hge_plh24.*;


public class LogInFrame extends javax.swing.JFrame {


    private DBManager dbm;
    private boolean dbStatus;
    private Users controlledUser;



    public LogInFrame() throws SQLException, ClassNotFoundException {
     initComponents();

     startDBconn ();
   }
// μέθοδος η οποία κάνει την σύνδεση με τη ΒΔ
  public void startDBconn () throws SQLException, ClassNotFoundException {
      dbStatus = true;

      try{
        dbm = new DBManager();
         }
      catch (Exception ex)
      {
            System.out.println(ex);
            dbStatus = false;
           
          }
      if (dbStatus==false)
        JOptionPane.showMessageDialog(new JFrame(), "Δεν είναι δυνατή η σύνδεση στην ΒΔ ");

  }



    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginLogoPanel = new javax.swing.JPanel();
        loginTextLabel = new javax.swing.JLabel();
        logoLabel = new javax.swing.JLabel();
        Login = new javax.swing.JPanel();
        usernameLabel = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        passwordField = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();

        setTitle("Οδηγός Πόλης");
        setBackground(new java.awt.Color(255, 255, 255));

        loginTextLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        loginTextLabel.setText("Σύνδεση στην εφαρμογή");
        loginTextLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        logoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI_3GE/logo.png"))); // NOI18N
        logoLabel.setToolTipText("");

        javax.swing.GroupLayout loginLogoPanelLayout = new javax.swing.GroupLayout(loginLogoPanel);
        loginLogoPanel.setLayout(loginLogoPanelLayout);
        loginLogoPanelLayout.setHorizontalGroup(
            loginLogoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginLogoPanelLayout.createSequentialGroup()
                .addGroup(loginLogoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginLogoPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(logoLabel))
                    .addGroup(loginLogoPanelLayout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(loginTextLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(57, Short.MAX_VALUE))
        );
        loginLogoPanelLayout.setVerticalGroup(
            loginLogoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginLogoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(loginTextLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        usernameLabel.setText("Όνομα Χρήστη : ");

        usernameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usernameFieldActionPerformed(evt);
            }
        });

        passwordLabel.setText("Κωδικός :");

        okButton.setLabel("ΟΚ");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout LoginLayout = new javax.swing.GroupLayout(Login);
        Login.setLayout(LoginLayout);
        LoginLayout.setHorizontalGroup(
            LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(LoginLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, LoginLayout.createSequentialGroup()
                        .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usernameLabel)
                            .addGroup(LoginLayout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addComponent(passwordLabel)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                            .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))))
                .addGap(80, 80, 80))
        );
        LoginLayout.setVerticalGroup(
            LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, LoginLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameLabel)
                    .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(LoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(okButton)
                .addGap(99, 99, 99))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginLogoPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(Login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(loginLogoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Login, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void usernameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usernameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_usernameFieldActionPerformed


    // μέθοδος η οποία «παίρνει» username και password από την φόρμα
//και ελέγχει αν υπάρχουν στην ΒΔ
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed


       String username =usernameField.getText();
            String password = passwordField.getText();
         
      if(username.equals("")){
           JOptionPane.showMessageDialog(new JFrame(), "Δεν έχετε καταχωρίσει username");
            return;
        } 
     if(password.equals(""))
            {
           JOptionPane.showMessageDialog(new JFrame(), "Δεν έχετε καταχωρίσει password");
           return;
        }
try {
        controlledUser = dbm.getUser(username, password);
        } catch (SQLException ex) {
            Logger.getLogger(LogInFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ControlledUserException ex) {
            Logger.getLogger(LogInFrame.class.getName()).log(Level.SEVERE, null, ex);
        }


      
        if(controlledUser instanceof Administrator) {
            AdministratorFrame x = new AdministratorFrame (dbm, controlledUser);
            x.setVisible(true);
            this.dispose();
        }
        else  if (controlledUser instanceof Users) {
                 UserFrame u = new  UserFrame (dbm, controlledUser);
            u.setVisible(true);
            this.dispose();
            }
        else{
             JOptionPane.showMessageDialog(new JFrame(), "Δεν βρέθηκαν τα στοιχεία"); 
            return;
            }

    }//GEN-LAST:event_okButtonActionPerformed


    
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Login;
    private javax.swing.JPanel loginLogoPanel;
    private javax.swing.JLabel loginTextLabel;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables

}
